package com.forum.impl.confession;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.ApiResult;
import com.common.vo.ConfessionCommentInfoVo;
import com.forum.mapper.confession.ConfessionWallCommentMapper;
import com.forum.mapper.confession.ConfessionWallCommentReplyMapper;
import com.forum.param.ConfessionQueryParam;
import com.common.utils.LocalContext;
import com.common.vo.ConfessionInfoVo;
import com.forum.mapper.confession.ConfessionWallMapper;
import com.forum.mapper.forum.ForumUserMapper;
import com.forum.param.ConfessionWallUploadParam;
import com.forum.pojo.confession.ConfessionWall;
import com.forum.pojo.forum.ForumUser;
import com.forum.service.confession.ConfessionWallService;
import com.forum.service.other.ThreadService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tsing
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ConfessionWallServiceImpl extends ServiceImpl<ConfessionWallMapper, ConfessionWall> implements ConfessionWallService {
    private final ConfessionWallMapper confessionWallMapper;
    private final ForumUserMapper forumUserMapper;
    private final ConfessionWallCommentMapper commentMapper;
    private final ConfessionWallCommentReplyMapper replyMapper;

    /**
     * stream流获取表白墙Vo集合
     * **/
    private List<ConfessionInfoVo> getConfessionInfoVos(List<ConfessionWall> confessionWalls){
        List<ConfessionInfoVo> confessionInfoVos = confessionWalls.stream().map(item -> {
            ConfessionInfoVo confessionInfoVo = new ConfessionInfoVo();
            ForumUser forumUser = forumUserMapper.selectById(item.getPublisherId());
            BeanUtils.copyProperties(item,confessionInfoVo);
            confessionInfoVo.setNickname(forumUser.getNickname());
            confessionInfoVo.setAvatar(forumUser.getAvatar());
            return confessionInfoVo;
        }).collect(Collectors.toList());
        return confessionInfoVos;
    }


    /**
     * 表白墙查询
     * **/
    @Override
    public ApiResult<Map<String,Object>> getConfessionList(ConfessionQueryParam param) {
        Map<String,Object> map = new HashMap<>(16);
        Page<ConfessionWall> page = new Page<ConfessionWall>();
        page.setCurrent(param.getPage()).setSize(param.getSize());
        LambdaQueryWrapper<ConfessionWall> queryWrapper = new LambdaQueryWrapper<>();
        String keyword = param.getKeyword();


        queryWrapper.like(keyword!=null,ConfessionWall::getSummary,param.getKeyword())
                .or()
                .like(keyword!=null,ConfessionWall::getContent,keyword)
                .orderBy(true,!param.getSort().equals("desc"),ConfessionWall::getCreateTime);

        Page<ConfessionWall> confessionWallPage = confessionWallMapper.selectPage(page, queryWrapper);
        List<ConfessionWall> records = confessionWallPage.getRecords();

        List<ConfessionInfoVo> confessionInfoVos = this.getConfessionInfoVos(records);

        map.put("total",confessionWallPage.getTotal());
        map.put("list",confessionInfoVos);
        return ApiResult.success(map);
    }

    /**
     * 获取当前用户的表白墙
     * **/
    @Override
    public ApiResult<Map<String, Object>> getConfessionByCurrentUser(ConfessionQueryParam param) {
        Map<String,Object> map = new HashMap<>(16);
        LambdaQueryWrapper<ConfessionWall> queryWrapper = new LambdaQueryWrapper<>();
        Page<ConfessionWall> page = new Page<>();
        page.setSize(param.getSize()).setCurrent(param.getPage());
        String keyword = param.getKeyword();
        Integer currentUserId = LocalContext.getCurrentUserId();

        queryWrapper
                .eq(ConfessionWall::getPublisherId,LocalContext.getCurrentUserId())
                .like(keyword!=null,ConfessionWall::getSummary,param.getKeyword())
                .like(keyword!=null,ConfessionWall::getContent,keyword)
                .orderBy(true,!param.getSort().equals("desc"),ConfessionWall::getCreateTime);
        Page<ConfessionWall> confessionWallPage = confessionWallMapper.selectPage(page, queryWrapper);
        List<ConfessionInfoVo> confessionInfoVos = this.getConfessionInfoVos(confessionWallPage.getRecords());
        map.put("total",confessionWallPage.getTotal());
        map.put("list",confessionInfoVos);
        return ApiResult.success(map);
    }

    /**
     * 获取表白墙详情
     * **/
    @Override
    public ApiResult<Map<String,Object>> getConfessionDetailById(Integer confessionId) {
        Map<String,Object> map = new HashMap<>(16);
        //获取表白墙
        ConfessionWall confessionWall = confessionWallMapper.selectById(confessionId);

        //使用线程池异步更新表白墙浏览和获取表白墙评论
        ThreadService threadService = new ThreadService();

        threadService.updateConfessionBrowse(confessionWallMapper,confessionWall);
        List<ConfessionCommentInfoVo> comments = threadService.getCommentByConfession(commentMapper, forumUserMapper, replyMapper,confessionId);

        //TODO 获取评论回复待做
        map.put("content",confessionWall);
        map.put("comments",comments);
        return ApiResult.success(map);
    }

    /**
     * 上传表白墙
     * **/
    @Override
    public ApiResult<String> confessionUpload(ConfessionWallUploadParam param) {
        ConfessionWall confessionWall = new ConfessionWall(LocalContext.getCurrentUserId(),param.getContent(),param.getImages(),param.getSummary());
        confessionWallMapper.insert(confessionWall);
        return ApiResult.success();
    }


}
